﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Project_1_refactored_
{
    class GameObject : characteristics
    {
        #region field declaration
        protected Texture2D t2dIdle;
        protected int frameCountIdle;
        protected Texture2D t2dRun;
        protected int frameCountRun;
        protected Texture2D t2dJump;
        protected int frameCountJump;
        protected Texture2D t2dAttack;
        protected int frameCountAttack;
        protected Texture2D t2dDie;
        protected int frameCountDie;

        protected Vector2 v2Position = new Vector2(50,500);       
        protected int currentFrame = 1; 
        //to determine is image is animated
        protected bool Animated = false;
        protected bool faceLeft = false;

        protected int frameWidth;
        protected int frameHeight;

        protected int worldWidth = 800;

        //for update method
        protected float frameRate = 0.1f;
        float fElapsed = 0.0f;

        //DIE
        protected bool dying = false;
        protected bool dieAnimationFinished = false;
        #endregion

        #region LoadContent Update Draw methods
        public void LoadContent(ContentManager Content, string assetIdle, string assetRun, string assetJump, string assetAttack, string assetDie, int[] frameCounts)
        {
            if (assetIdle != "")
                t2dIdle = Content.Load<Texture2D>(assetIdle); frameCountIdle = frameCounts[0];
            if (assetRun != "")
                t2dRun = Content.Load<Texture2D>(assetRun); frameCountRun = frameCounts[1];
            if (assetJump != "")
                t2dJump = Content.Load<Texture2D>(assetJump); frameCountJump = frameCounts[2];
            if (assetAttack != "")
                t2dAttack = Content.Load<Texture2D>(assetAttack); frameCountAttack = frameCounts[3];
            if (assetDie != "")
                t2dDie = Content.Load<Texture2D>(assetDie); frameCountDie = frameCounts[4];
        }

        public void Update(GameTime gameTime, Vector2 speed, Vector2 direction)
        {
            if(Animated)
                v2Position += direction * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
        }

        public void Update(GameTime gameTime, string assetName)
        {
            int frameCount = 0;
            switch (assetName)
            {
                case "Idle":
                    frameCount = frameCountIdle;
                    break;
                case "Run":
                    frameCount = frameCountRun;
                    break;
                case "Jump":
                    frameCount = frameCountJump;
                    break;
                case "Attack":
                    frameCount = frameCountAttack;
                    break;
                case "Die":
                    frameCount = frameCountDie;
                    break;
            }

            if (Animated)
            {
                if(!dying)
                    frameLoop(gameTime, frameCount);
                if (dying)
                    frameLoopDie(gameTime);
            }
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(t2dIdle, v2Position, Color.White);
        }
        #endregion

        #region Helper methods
        //helper for collision detection
        public Rectangle collisionBOX()
        {
                return new Rectangle((int)v2Position.X, (int)v2Position.Y, frameWidth, frameHeight);
        }
        //helper for draw
        protected Rectangle getSourceRect()
        {
            return new Rectangle(
                frameWidth * currentFrame,
                0,
                frameWidth,
                frameHeight);
        }

        protected void frameLoop(GameTime gameTime, int frameCount)
        {
            fElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (fElapsed >= frameRate)
            {
                currentFrame = (currentFrame + 1) % frameCount;
                fElapsed = 0f;
            }
        }

        protected void frameLoopDie(GameTime gameTime)
        {
            fElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (fElapsed >= frameRate && !dieAnimationFinished)
            {
                if (currentFrame == frameCountDie)
                {
                    dieAnimationFinished = true;
                    currentFrame = frameCountDie;
                }
                else
                {
                    fElapsed = 0f;
                }
            }
        }
        #endregion
    }
}
